/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.spice.servlet.dispatcher;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spice.servlet.core.components.Resource;
import org.spice.servlet.dispatcher.handler.RequestListeners;
import org.spice.servlet.inject.InstanceWrapper;
import org.spice.servlet.inject.strategy.Converter;

/**
 * @author Karthik Rajkumar
 * @version (included from the RED-CHILLIES version)
 */
public class SpiceServlet extends Spice {

	private static final long serialVersionUID = -4396985036726350005L;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(SpiceServlet.class);

	@Override
	protected 
	void initializeSpice(final ServletConfig servletConfig) {
		StringBuffer path = new StringBuffer();
		
		final ServletContext servletContext = servletConfig.getServletContext();
		SpiceDocumentFactory factory = new SpiceDocumentFactory();
		final XSDElements xsdElements = factory.configureSpice(servletContext.getRealPath("/") , servletContext.getInitParameter("construct"));
		XSDHolder holder = XSDHolder.getInstance();
		holder.setXsdElements(xsdElements);
		String configuredPath = xsdElements.getScanpath();
		LOGGER.info("The configured class path are " + configuredPath);
		configuredPath = configuredPath.replace(".", "/");
		configuredPath = configuredPath.concat("/");
		

		if (configuredPath != null)
			path.append("classpath*:").append(configuredPath).append("**/*.class");
		else
			path.append("classpath*:/**/*.class");

		if (LOGGER.isInfoEnabled())
			LOGGER.info("The classpath to be scanned are " + path.toString());
		
	
		
		
		final Converter converter = new Converter();
		final Resource resource = converter.getResources(path.toString());
			try {
				new InstanceWrapper().getInjection(resource);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 

	}


	/**
	 * All the inherited methods are calls the method in order to make sure that
	 * all the callbacks are handled.
	 * 
	 * @param request
	 *            the {@link javax.servlet.http.HttpServletRequest} object that
	 *            contains the request the client made of the servlet
	 * @param response
	 *            the {@link javax.servlet.http.HttpServletResponse} object that
	 *            contains the response the servlet returns to the client
	 * @throws java.io.IOException
	 *             if an input or output error occurs while the servlet is
	 *             handling the HTTP request
	 * @throws javax.servlet.ServletException
	 *             if the HTTP request cannot be handled
	 * @see SpiceServlet #dispatch
	 */
	@Override
	protected void dispatch(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestListeners.listenToRequest(request, response, getServletConfig());

	}
}
